﻿using MesBSW.BLL.Mes;
using MesBSW.DAL.MesDB;
using MesBSW.View.Dialog.Mes;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace MesBSW.View.Mes
{
    public partial class RepairReasonFrm : UIPage
    {
        #region "内部变量"
        private ExcepOrderControler _ctrl = new ExcepOrderControler();

        private List<SiteInfo> _site_list = new List<SiteInfo>();
        #endregion

        public RepairReasonFrm()
        {
            InitializeComponent();
        }


        #region "控件事件"
        /// <summary>
        /// 界面读入时，查询所有站点，并产生站点树
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RepairReasonFrm_Load(object sender, EventArgs e)
        {
            this._site_list = this._ctrl.FindAllSiteInfo();
            this.InitTreeViewForSiteInfo(this._site_list);
        }

        /// <summary>
        /// 选择树控件节点时，显示该站点对应的维修原因
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tvwSite_AfterSelect(object sender, TreeViewEventArgs e)
        {
            this.SearchRepairReasonForSite(e.Node.Text.Trim());
        }
        #endregion

        #region "内部方法"
        /// <summary>
        /// 初始化站点树
        /// </summary>
        /// <param name="siteList"></param>
        private void InitTreeViewForSiteInfo(List<SiteInfo> siteList)
        {
            this.tvwSite.Nodes.Clear();
            if (siteList != null && siteList.Count > 0)
            {
                var parQuery = siteList.Where(s => s.IsFinal != 1).OrderBy(s => s.SiteCode);
                if (parQuery != null)
                {
                    foreach (var parent in parQuery)
                    {
                        TreeNode parNode = new TreeNode();
                        parNode.Tag = parent.SiteCode;
                        parNode.Text = parent.SiteName;
                        var childQuery = siteList.Where(s => s.ParentID == parent.ID).OrderBy(s => s.SiteCode);
                        if (childQuery != null)
                        {
                            foreach (var child in childQuery)
                            {
                                TreeNode childNode = new TreeNode();
                                childNode.Tag = child.SiteCode;
                                childNode.Text = child.SiteName;
                                parNode.Nodes.Add(childNode);
                            }
                        }
                        this.tvwSite.Nodes.Add(parNode);
                    }
                }
            }
        }


        /// <summary>
        /// 将维修原因从List转换成DataTable
        /// </summary>
        /// <param name="reasonList"></param>
        /// <returns></returns>
        private DataTable ChangeReasonListToDataTable(List<RepairReason> reasonList)
        {
            DataTable resDt = new DataTable();
            resDt.Columns.Add("id", typeof(int));
            resDt.Columns.Add("site_id", typeof(int));
            resDt.Columns.Add("site_name", typeof(string));
            resDt.Columns.Add("reason", typeof(string));
            resDt.Columns.Add("is_del", typeof(bool));

            if (reasonList != null && reasonList.Count > 0)
            {
                foreach (var item in reasonList)
                {
                    DataRow addRow = resDt.NewRow();
                    addRow["id"] = item.ID;
                    addRow["site_id"] = item.SiteID;
                    addRow["site_name"] = item.SiteName;
                    addRow["reason"] = item.Reason;
                    addRow["is_del"] = item.IsDel == 1 ? true : false;
                    resDt.Rows.Add(addRow);
                }
            }
            return resDt;
        }


        /// <summary>
        /// 根据站点名查询该站点的所有维修原因
        /// </summary>
        /// <param name="siteName"></param>
        private void SearchRepairReasonForSite(string siteName)
        {
            List<RepairReason> tmpList = new List<RepairReason>();
            if (this._site_list != null && this._site_list.Count > 0)
            {
                var query = this._site_list.Where(s => s.SiteName == siteName);
                if (query != null && query.Count() > 0)
                {
                    tmpList = this._ctrl.SearchRepairReasonForSite(query.First<SiteInfo>().ID);
                }
            }

            this.dgvReason.DataSource = this.ChangeReasonListToDataTable(tmpList);
        }
        #endregion

        private void btnAddStand_Click(object sender, EventArgs e)
        {
            SiteInfo currSite = null;
            if (this.tvwSite.SelectedNode != null && this._site_list != null)
            {
                var query = this._site_list.Where(s => s.SiteName == this.tvwSite.SelectedNode.Text.Trim());
                if (query != null && query.Count() > 0)
                {
                    currSite = query.First<SiteInfo>();
                }
            }

            if (currSite != null)
            {
                AddRepairReasonDia tmpDia = new AddRepairReasonDia(currSite);
                tmpDia.ShowDialog();

                this.SearchRepairReasonForSite(currSite.SiteName);
            }
            else
            {
                UIMessageBox.ShowWarning("请在左边列表中选择站点后再新增！");
            }
        }
    }
}
